package com.framework.loippi.controller.admin;


import com.framework.loippi.controller.GenericController;
import com.framework.loippi.entity.Article;
import com.framework.loippi.mybatis.paginator.domain.Order.Direction;
import com.framework.loippi.service.ArticleCategoryService;
import com.framework.loippi.service.ArticleService;
import com.framework.loippi.support.Message;
import com.framework.loippi.support.Page;
import com.framework.loippi.support.Pageable;
import com.framework.loippi.utils.ParameterUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Controlelr - 文章管理
 *
 * @author Loippi Team
 * @version 1.0
 */
@Controller("adminArticleController")
@RequestMapping("/admin/article")
public class ArticleController extends GenericController {

    @Resource
    private ArticleService articleService;

    @Resource
    private ArticleCategoryService articleCategoryService;

    /**
     * 添加
     *
     * @param type 类型(1-帮助文章，2-系统文章)
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model, Integer type) {
        model.addAttribute("type", 2);
        return "/admin/article/add";
    }

    /**
     * 添加
     *
     * @param type 类型(1-帮助文章，2-系统文章)
     * @param id
     */
    @RequestMapping(value = "/help_add", method = RequestMethod.GET)
    public String addHelp(ModelMap model, Integer type) {
        model.addAttribute("type", 1);
        return "/admin/article/help_add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Article article, RedirectAttributes redirectAttributes) {
        article.setCreateDate(new java.util.Date());
        if (article.isShow()) {
            article.setShow(true);
            article.setPublishDate(new Date());
        }
        articleService.save(article);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        if (article.getType() == 1) {
            return "redirect:help_list.jhtml";
        }
        return "redirect:list.jhtml";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        Article article = articleService.find(id);
        model.addAttribute("article", article);
        return "/admin/article/edit";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/help_edit", method = RequestMethod.GET)
    public String helpEdit(Long id, ModelMap model) {
        Article article = articleService.find(id);
        model.addAttribute("systemArticle", article);
        return "/admin/article/help_edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Article article, RedirectAttributes redirectAttributes) {
        articleService.update(article);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        if (article.getType() == 1) {
            return "redirect:help_list.jhtml";
        }
        return "redirect:list.jhtml";
    }

    /**
     * 系统文章列表
     *
     * @param pageable
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, HttpServletRequest request, ModelMap model) {
        Map<String, Object> paramter = ParameterUtils.getParametersMapStartingWith(request, "filter_");
        paramter.put("type", 2);
        pageable.setParameter(paramter);
        pageable.setOrderProperty("CREATE_DATE");
        pageable.setOrderDirection(Direction.DESC);
        Page<Article> page = articleService.findByPage(pageable);
        model.addAttribute("page", page);
        model.addAttribute("paramter", paramter);
        return "/admin/article/list";
    }


    /**
     * 帮助文章列表
     */
//	@RequiresPermissions("admin:article:list")
    @RequestMapping(value = "/help_list", method = RequestMethod.GET)
    public String helpList(Pageable pageable, HttpServletRequest request, ModelMap model) {
        Map<String, Object> paramter = ParameterUtils.getParametersMapStartingWith(request, "filter_");
        paramter.put("type", 1);
        pageable.setParameter(paramter);
        pageable.setOrderProperty("CREATE_DATE");
        pageable.setOrderDirection(Direction.DESC);
        Page<Article> page = articleService.findByPage(pageable);
        model.addAttribute("page", page);
        model.addAttribute("paramter", paramter);
        return "/admin/article/help_list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody
    Message delete(Long[] ids) {
        for (Long id : ids) {
            articleService.delete(id);
        }
        return SUCCESS_MESSAGE;
    }

}
